import {
  MessageBox,
  Message
} from 'element-ui'

const mobile = (phoneNo) => {
  const reg = /^1(3[0-9]|4[579]|5[0-35-9]|7[0135678]|8[0-9]|66|9[89])\d{8}$/
  return reg.test(phoneNo)
}
// 固话 座机
const landLine = (val) => {
  const reg1 = /\d{3}-\d{8}|\d{4}-\d{7}/
  const reg2 = /^1(3[0-9]|4[579]|5[0-35-9]|7[0135678]|8[0-9]|66|9[89])\d{8}$/
  return reg1.test(val) || reg2.test(val)
}
// 银行卡号
const bank = (val) => {
  const reg = /^[1-9]\d{9,29}$/
  return reg.test(val)
}
const password = (password) => {
  const reg = /^(?![A-Za-z]+$)(?!\d+$)(?![\W_]+$)\S{6,12}$/
  return reg.test(password)
}
const telephone = (telephoneNo) => {
  const reg = /^[0-9]+(-[0-9]+)*$/
  return reg.test(telephoneNo)
}
const email = (emailStr) => {
  const reg = /^([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)?@([a-zA-Z0-9_-])+(\.([a-zA-Z0-9_-])+)+$/
  return reg.test(emailStr)
}
const empty = (obj) => {
  if (typeof (obj) === 'string') {
    if (obj.replace(/\s+/g, '') === '') {
      return true
    }
  }
  if (obj === undefined || obj === null || obj === '') {
    if (typeof (obj) === 'string') {
      if (obj.replace(/\s+/g, '') === '') {
        return true
      }
    } else {
      return true
    }
  } else if ((obj instanceof Array) || (obj instanceof Set)) {
    return obj.length == 0
  } else if (obj instanceof Map) {
    return obj.size == 0
  } else if (json(obj)) {
    return Object.keys(obj).length == 0
  }
  return false
}
const blank = (str) => {
  if (str === undefined || str === null || str === '') {
    return true
  }
  const type = typeof str
  if (type == 'string') {
    return str.trim() === ''
  } else {
    return false
  }
}
const hasBlank = (str) => {
  if (str === undefined || str === null || str === '') {
    return false
  }
  const type = typeof str
  if (type == 'string') {
    const reg = /\s+/
    return reg.test(reg)
  } else {
    return false
  }
}
const json = (obj) => {
  const isJson = typeof (obj) === 'object' &&
    Object.prototype.toString.call(obj).toLowerCase() == '[object object]' &&
    !obj.length
  return isJson
}
// 整数
const integer = (obj) => {
  return (typeof obj === 'number' || (typeof obj === 'string' && !empty(obj))) && obj % 1 === 0
}
// 正整数
const positiveInteger = (obj) => {
  return /^[1-9]\d{0,}$/.test(obj)
}
const number = (obj) => {
  return !empty(obj) && !isNaN(obj)
}
// 限定数字(数据, 整数位数, 小数位数)
const numberFormat = (obj, IntegerLength, decimalLength) => {
  if (!number(obj)) {
    return false
  }
  const objStr = obj + ''
  if (!empty(IntegerLength)) {
    if (objStr.split('.')[0].length > IntegerLength) {
      return false
    }
  }
  if (!empty(decimalLength)) {
    if (objStr.indexOf('.') > -1 && objStr.split('.')[1].length > decimalLength) {
      return false
    }
  }
  return true
}
// 修复小数点精度问题 1 元转分  2 分转元
const fixDot = (type, num) => {
  // var reg = /(^0\.[1-9]\d?$)|(^0\.\d[1-9]?$)|(^[1-9]\d{0,7}(\.\d{0,2})?$)/;
  // if (!reg.test(data[rule[i].name])) {
  //   this.error = rule[i].errorMsg;
  //   return false;
  // }
  if (type == 1) {
    return Math.round(num * 100)
  }
  if (type == 2) {
    return num / 100
  }
}
const idCard = (cardNum) => {
  const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  return reg.test(cardNum)
}

// 提示
const tip = (message, time = 2000, type = 'info') => {
  Message({
    message,
    type,
    duration: time
  })
}

// 七牛云地址
const sevenDomain = 'http://119.3.69.218/admin/v1/cms-resource/upload'
const sevenDomain2 = 'http://up-z2.qiniup.com'
const Is = {
  sevenDomain, // 七牛云地址
  sevenDomain2, // 七牛云地址
  password, // 密码正则
  mobile, // 是否为中国境内手机号码
  landLine, // 座机
  bank, // 银行卡号
  telephone, // 是否为中国境内电话号码
  email, // 是否为邮箱
  empty, // 字符串、数组、集合、map、json的空判断
  blank, // 字符串是否全为空字符,
  hasBlank, // 字符串是否含有空字符,
  json, // 是否是json对象
  integer, // 是否为整数
  positiveInteger, // 是否为正整数
  number, // 是否为数字
  numberFormat, // 数字格式校验
  idCard, // 身份证号
  fixDot, // 小数转换
  tip // 提示
}
export default Is
